package org.sam.swing.table.editor;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.TreeSet;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.table.DefaultTableCellRenderer;

import org.sam.swing.JSPanel;
import org.sam.swing.annotation.Id;
import org.sam.swing.table.JSTable;
import org.sam.swing.table.JSTableBuilder;
import org.sam.swing.table.JSTableColumn;
import org.sam.swing.table.JSTableColumnModel;
import org.sam.swing.table.JSTableModel;
import org.sam.swing.table.JSTableModelEvent;
import org.sam.swing.table.JSTableModelLinster;
import org.sam.swing.table.defaultImpl.JSTableArrayBuilderImpl;
import org.sam.swing.table.defaultImpl.JSTableModelArrayAdapter;
import org.sam.swing.table.renderer.JSTableRowNumberRenderer;
import org.sam.swing.table.tools.JSTableToolBar;

/**
 * 多值输入窗口控件
 * 
 * @author sam
 *
 */
public class JSTableCollectionSelectPanel<T> extends JSPanel {

	private static final long serialVersionUID = 1235845013506459394L;

	/**
	 * 表格数据对象
	 */
	protected JSTableModel<Collection<Object[]>> tableModel;

	/**
	 * 数据列操作对象
	 */
	protected JSTableColumnModel colModel;

	/**
	 * 当前操作的表格
	 */
	@Id("table")
	protected JSTable table;

	/**
	 * 确定按钮
	 */
	@Id("btnOk")
	protected JButton btnOk;

	/**
	 * 取消按钮
	 */
	@Id("btnCancel")
	protected JButton btnCancel;

	/**
	 * 当前操作的结果值
	 */
	private Collection<T> value;

	/**
	 * 单元格默认值
	 */
	private T defaultValue;

	/**
	 * 当前操作的结果值
	 * 
	 * @return
	 */
	public Collection<T> getValue() {
		return value;
	}

	/**
	 * 当前操作的结果值
	 * 
	 * @param value
	 */
	public void setValue(Collection<T> value) {
		this.value = value;
	}

	/**
	 * 初始化控件
	 */
	public JSTableCollectionSelectPanel() {
		this(new TreeSet<>());
	}

	/**
	 * 单元格默认值
	 * 
	 * @return
	 */
	public T getDefaultValue() {
		return defaultValue;
	}

	/**
	 * 单元格默认值
	 * 
	 * @param defaultValue
	 */
	public void setDefaultValue(T defaultValue) {
		this.defaultValue = defaultValue;
		if (this.colModel != null) {
			JSTableColumn column = (JSTableColumn) this.colModel.getColumn(1);
			column.setDefaultValue(defaultValue);
		}
	}

	/**
	 * 多行数值编辑功能
	 * 
	 * @param value
	 * @param cellEdit
	 */
	public JSTableCollectionSelectPanel(Collection<T> value) {
		this(value, null);
	}

	/**
	 * 
	 * @param value
	 * @param col
	 * @param defaultValue
	 */
	public JSTableCollectionSelectPanel(Collection<T> value, T defaultValue) {
		this.setValue(value);
		this.initComponet(defaultValue);
	}

	/**
	 * 初始化控件显示
	 */
	protected void initComponet(T defaultValue) {
		this.setLayout(new BorderLayout());

		DefaultTableCellRenderer renderl = new DefaultTableCellRenderer();

		JSTableColumn c1 = new JSTableColumn();
		c1.setModelIndex(0);
		c1.setHeaderValue("序号");
		c1.setTitle("序号");
		c1.setIdentifier(0);
		c1.setMaxWidth(35);
		c1.setCellRenderer(new JSTableRowNumberRenderer());
		c1.setEditable(false);

		JSTableColumn c2 = new JSTableColumn();
		c2.setModelIndex(1);
		c2.setHeaderValue("值");
		c2.setIdentifier(1);
		c2.setTitle("值");
		c2.setMinWidth(175);
		c2.setDefaultValue(defaultValue);
		c2.setCellRenderer(renderl);

		JSTableBuilder<Collection<Object[]>> builder = new JSTableArrayBuilderImpl(c1, c2) {

			@Override
			public JSTableModelLinster<Collection<Object[]>> buildModelLinster(
					JSTableModel<Collection<Object[]>> tableModel) throws Exception {
				JSTableModelArrayAdapter adapter = new JSTableModelArrayAdapter() {

					/**
					 * 检索数据
					 */
					@Override
					public Collection<Object[]> onRetrieve() throws Exception {
						Collection<T> orignal = getValue();
						if (orignal == null || orignal.isEmpty())
							return null;

						List<Object[]> result = new ArrayList<>();
						for (T v : orignal) {
							Object[] item = new Object[2];
							item[0] = 0;
							item[1] = v;
							result.add(item);
						}
						return result;
					}

					/**
					 * 提交之前回填到set中
					 */
					@SuppressWarnings("unchecked")
					@Override
					public void beforeUpdate(JSTableModelEvent event) throws Exception {
						Collection<T> orginal = getValue();
						if (orginal == null)
							orginal = new TreeSet<>();

						orginal.clear();

						List<Object[]> datas = (List<Object[]>) event.getTableModel().getDatas();
						if (datas != null && !datas.isEmpty()) {
							for (Object[] item : datas) {
								orginal.add((T) item[1]);
							}
						}
						setValue(orginal);
						super.beforeUpdate(event);
					}
				};
				tableModel.setTableModelLinster(adapter);
				return adapter;
			}
		};

		try {

			tableModel = builder.getTableModel();
			colModel = builder.getTableColumnModel();
			table = builder.buildTable();
			table.setName("table");

			if (c2.getVerficator() != null) {
				// 开启列校验
				tableModel.setTableColumnVerificatior(true);
				// 开启校验高亮显示
				table.setVerificatHightLight(true);
			}

			// 每次数据变更后刷新地图
			tableModel.retrieve();
			tableModel.setEditable(true);

		} catch (Exception e) {
			e.printStackTrace();
		}

		JScrollPane scrollPane = new JScrollPane(table);
		this.add(scrollPane, BorderLayout.CENTER);

		// 以下是测试表格显示风格的操作
		JSTableToolBar toolBar = new JSTableToolBar(table);
		toolBar.enableAdd();
		toolBar.enableDel();
		toolBar.enableSearch();
		add(toolBar, BorderLayout.NORTH);

		// 底部按钮
		JPanel panelToolBar = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		btnOk = new JButton("确定");
		btnOk.setName("btnOk");
		btnOk.addActionListener(e -> {
			try {
				tableModel.update();
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		});
		panelToolBar.add(btnOk);

		btnCancel = new JButton("取消");
		btnCancel.setName("btnCancel");
		panelToolBar.add(btnCancel);

		this.add(panelToolBar, BorderLayout.SOUTH);
	}
}
